package project.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import project.bean.Project;
import project.factory.ProjectFactory;
import project.service.ProjectService;

public class ProjectAction extends ActionSupport implements SessionAware{
	private Project project=new Project();
	private List<Project> projectlist=new ArrayList<Project>();
	private Map session;
	private int pagenow;
	
	public int getPagenow(){
		return pagenow;
	}
    public  void setPagenow(int pagenow){
    	this.pagenow=pagenow;
    }
	

	

	public void setProject(Project projectbean){
		this.project=projectbean;
	}
	
	public Project getProject(){
		return project;
	}
	
	public List<Project> getProjectlist(){
		return projectlist;
	}
	
	public void setProjectlist(List<Project> projectlist){
		this.projectlist=projectlist;
	}
	
	public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public String modifyProject() throws Exception {
		if(project.getItem().trim().equalsIgnoreCase("")){
			this.addActionError("modifyProject error");
			return INPUT;
		}
	   if(project.getProjectname().trim().equalsIgnoreCase("")){
		   this.addActionError("modifyProject error");
		   return INPUT;
	   }

		ProjectService projectservice=ProjectFactory.getInstance().creatProjectService();
        if(!projectservice.ModifyProject(project)){
        	this.addActionError("modifyProject error");
        	return INPUT;
        }
		
		Map session = ActionContext.getContext().getSession();
		session.remove("project");
		return SUCCESS;
	}
	
	public String deleteProject() throws Exception {
		ProjectService projectservice =ProjectFactory.getInstance().creatProjectService();
		if (!projectservice.DeleteProject(project)) {
			this.addActionError("deleteProject error");
			return INPUT;
		} else {
			this.addActionError("deleteProject error");
			return SUCCESS;
		}
	}
	
	public String addProject() throws Exception {	
		
		ProjectService projectservice=ProjectFactory.getInstance().creatProjectService();
		if (project.getItem().trim().equalsIgnoreCase("")) {
			this.addActionError("addProject error");
			return INPUT;
		}
		if (project.getProjectname().trim().equalsIgnoreCase("")) {
			this.addActionError("addProject error");
			return INPUT;
		}
		if(!projectservice.AddProject(project)){
			this.addActionError("addProject error");
			return INPUT;
		}
		this.addActionError("addProject error");
		return SUCCESS;
	}

	 public String showallProject() throws Exception{
		 ProjectService projectservice=ProjectFactory.getInstance().creatProjectService();
		 projectlist=projectservice.ShowAllProject(getPagenow(),6);
		 if(getPagenow()==0){
			 int total=projectservice.CountAllProject();
			 int page=0;
			 do {
					total = total - 6;
					page++;
				} while (total > 0);
				this.session.put("projecttotalpage", page);
		 }
		 if(projectlist.size()==0){
			 this.addActionError("showallproject error");
			return INPUT;
		 }else{
			 return  SUCCESS; 
		 }
	 }
	 
	 public String showoneProject() throws Exception{
			ProjectService projectService =ProjectFactory.getInstance().creatProjectService();
			project= projectService.ShowoneProject(project);
			if (project == null) {
				this.addActionError("Error!");
				return INPUT;
			} else {
				this.session.put("project", project);
				return SUCCESS;
			}
		}
	 }
